package com.qipay.configuration.service.impl;

import com.qipay.baas.model.Configuration;
import com.qipay.configuration.config.ConfigurationEnum;
import com.qipay.configuration.service.ConfigurationService;
import com.qipay.mapper.ConfigurationMapper;
import io.jsonwebtoken.lang.Assert;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Creator: Yao
 * Date:    2017/9/30
 * For:
 * Other:
 */
@Service
public class ConfigurationServiceImpl implements ConfigurationService {

    @Autowired
    ConfigurationMapper mapper;


    @Override
    public Map<Byte, String> map() {

        // init
        Map<Byte, String> map = new HashMap<>();

        // query
        List<Configuration> list = mapper.selectAll();
        if (CollectionUtils.isNotEmpty(list)) {
            map = list.stream().collect(Collectors.toMap(p -> p.getType(), p -> p.getValue()));
        }

        return map;
    }

    @Override
    public String select(byte type) {

        // query
        Configuration conf = mapper.selectByType(type);

        // check
        Assert.notNull(conf, "未找到指定配置");

        return conf.getValue();
    }
}
